<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Home\CommonController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Link;
use DB;
use App\Models\Carousel;
use App\Models\Category;
use App\Models\UserDetail;
class QuestionController extends CommonController
{

     /**
     * 处理前台问题数据
     */
    static public function getCatePid($pid = 0)
    {
        $data = DB::table('zy_category')->where('pid',$pid)->get();
        $arr = [];
        foreach ($data as $key => $value)
        {
            $value['sub'] = self::getCatePid($value['id']);
            $arr[] = $value;
        }
        return $arr;
    }


     // 处理分类
    static public function getCates()
    {
        // $data = DB::select("select *,concat(path,',',id) as paths from cate order by paths");
        $data = DB::table('zy_category')->select(DB::raw("*,concat(path,'-',id) as paths"))->orderBy('paths')->get();
        foreach ($data as $key => $value) {
            // 统计一个字符串在另一个字符串出现次数
            $len = substr_count($value['path'],'-');
            // $data[$key]['name']
            // |----男装
            // 重复的使用一个字符串
           $data[$key]['cate_name'] = str_repeat('|----',$len).$value['cate_name'];
        }
        return $data;
    }
    /**
     *  返回问题提问页
     *
     * @return \Illuminate\Http\Response
     */
    public function index($id = '')
    {
        $weq=DB::table('zy_category')->where('pid','=','0')->get();
        // dd($data);
        //获取分类表中的数据
       // $weq=self::getCatePid();
        $data = self::getCates();

        // 调用getCatePid()方法
        $cate_data = self::getCatePid();

        return view('home.question.create',compact('cate_data','weq','id'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * 二级分类的下拉项
     */
    public function create(Request $request)
    {
        //是一个一维数组
       $id=$request->all();
       //获取到的data是一个二维数组
       $data=DB::table('zy_category')->where('pid',$id['id'])->get();

       $str=json_encode($data);
        return $str;
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取表单提交的值
        $data=$request->except('_token');


        //判断用户是否登录
        if($data['username1']==null)
        {
            echo "<script>alert('请前去登录');location.href='/home'</script>";
            return;
        }
        if($data['art_content']==null || $data['q_title']==null)
        {
            echo "<script>alert('问题内容输入不能为空');location.href='/home/question'</script>";
            return;
        }
        $q_title=$data['q_title'];
        $q_time=$data['q_time'];
        $status=$data['status'];
        $uid=$data['uid'];
        $username=$data['username1'];
        // dd($username);
        $q_content=$data['art_content'];

        $cate_id=$data['cate_id'];
        $cate_pid=$data['cate_pid'];
        $tag_id = $data['tag_name'];
        // dd($tag_id);

        //判断是否有分类与标签名
        if($cate_pid==null)
        {
            echo "<script>alert('请选择分类');location.href='/home/question'</script>";
            return;
        }
        if(!$tag_id)
        {
            echo "<script>alert('请填写标签名');location.href='/home/question'</script>";
            return;
        }
        // //执行标签名的添加
        // $id=DB::table('zy_tag')->insertGetId(['tag_name'=>$tag_name,'cid'=>$cate_pid]);
        // //判断是否添加成功
        // if(!$id)
        // {
        //    return back()->with('error','标签添加失败');
        // }

        //执行问题的添加
         $res=DB::table('zy_question')->insertGetId([
                    'uid'=>$uid,
                    'q_title'=>$q_title,
                    'q_content'=>$q_content,
                    'status'=>$status,
                    'q_time'=>$q_time,
                    'cate_id'=>$cate_id,
                    'username'=>$username,
                    'cate_pid'=>$cate_pid

        ]);

        //判断添加问题列表是否成功
       if($res)
        {

            // 添加标签
            $tag_id = DB::table('zy_question_tag')->insert(['qid'=>$res,'tid'=>$tag_id]);


            //如果用户提问成功userdetails表q_conut自增1
            UserDetail::where('uid',$uid)->increment('q_count');
            $score=DB::table('zy_score')->where('uid',$uid)->first();
            $score1=$score['score']+5;
            $re=DB::table('zy_score')->where('uid',$uid)->update(['score'=>$score1]);

            if($re)
            {
                return view('home.question.jump')->with([
                    'message'=>'提交成功,经验+10,积分+5',
                    'url' =>'/home/list?id='.$cate_pid,
                    'jumpTime'=>2,
                ]);
            }

        }else{
            return back()->with('error','提问失败');
        }

    }
    /**
     * 添加标签
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function tag_name(Request $request)
    {
        //是一个一维数组
        $id = $request->all();

        //获取到的data是一个二维数组
        $data = DB::table('zy_tag')->where('cid',$id['id'])->get();
        $str = json_encode($data);
        // dd($str);
        return $str;
    }
    /**
     * 返回问题列表
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function list(Request $request)
    {

        // 查询数据并分页

        //前台分类无限极分类获得的id
        // $id=$request->Input('id');
        $id1=$request->all();
        $id=$id1['id'];
        // dd($id);
         // 查询数据并分页
        $search = $request -> input('search','');

        // $count = $request -> input('count',3);
        $request = $request -> all();
        $data = DB::table('zy_question')->where('cate_pid',$id)->orwhere('cate_id',$id)->where('status','=','0')->where('q_title','like','%'.$search.'%')->orderBy('q_time','desc')->paginate(10);
        // dd($data);
        $tid = [];
        // 遍历问题查找标签关系表
        foreach($data as $k=>$v)
        {
            $qid = $v['id'];

            $tag = DB::table('zy_question_tag')->where('qid',$qid)->get();
            foreach($tag as $kk=>$vv)
            {
                $v['tid'] = $vv['tid'];
                $tid[] = $v;

            }
        }
        // dd($tid);
        // 遍历问题标签查找标签
        $tag_name = [];
        foreach($tid as $m=>$n)
        {
            $tid = $n['tid'];
            $tagname = DB::table('zy_tag')->where('tag_id',$tid)->get();

            foreach($tagname as $s=>$t)
            {
                $n['tag_name'] = $t['tag_name'];
                $tag_name[] = $n;
            }
        }
        // 获取分类的名称
        $cate_p = DB::table('zy_category')->where('id',$data[0]['cate_id'])->first();
        $cate_s = DB::table('zy_category')->where('id',$data[0]['cate_pid'])->first();
        // dd($tag_name);
        return view('home.question.index',compact('data','tag_name','request','id','cate_s','cate_p'));

    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
